package seekting.demo2017.activity;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.AsyncTaskLoader;
import android.content.Loader;
import android.os.Bundle;
import android.util.Log;

/**
 * Created by seekting on 17-7-7.
 */

public class LoaderManagerActivity extends Activity {

    private Loader<String> mLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoader = new AsyncTaskLoader<String>(this) {

            @Override
            public void registerListener(int id, OnLoadCompleteListener<String> listener) {
                super.registerListener(id, listener);
                Log.d("seekting","LoaderManagerActivity.registerListener()");
            }

            @Override
            protected void onReset() {
                super.onReset();
                Log.d("seekting", "LoaderManagerActivity.onReset()");
            }

            @Override
            protected void onStartLoading() {
                super.onStartLoading();
                forceLoad();
            }

            @Override
            public void onCanceled(String data) {
                super.onCanceled(data);
                Log.d("seekting", "LoaderManagerActivity.onCanceled()" + data);
            }

            @Override
            public void deliverResult(String data) {
                super.deliverResult(data);
                Log.d("seekting","LoaderManagerActivity.deliverResult()");
            }

            @Override
            public String loadInBackground() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d("seekting", "LoaderManagerActivity.loadInBackground()");
                return "hello";
            }
        };
        LoaderManager loaderManager = getLoaderManager();
        loaderManager.initLoader(1, null, new android.app.LoaderManager.LoaderCallbacks<String>() {
            @Override
            public Loader<String> onCreateLoader(int id, Bundle args) {
                return mLoader;
            }

            @Override
            public void onLoadFinished(Loader<String> loader, String data) {
                Log.d("seekting", "LoaderManagerActivity.onLoadFinished()" + data);

            }

            @Override
            public void onLoaderReset(Loader<String> loader) {
                Log.d("seekting", "LoaderManagerActivity.onLoaderReset()");

            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getLoaderManager().destroyLoader(1);
    }
}
